const { JSONCookie } = require("cookie-parser");
var express = require("express");
var router = express.Router();
var pool = require("../pool");


//用户注册
router.post("/register", (req, res) => {
	var { uname, upwd } = req.body;
	var sql = "select * from mx_user where uname=?";
	pool.query(sql, [uname], (err, result) => {
		if (err) throw err;
		if (result.length > 0) {
			res.send({ code: 201, msg: '用户名存在' });
		} else {
			var sql1 = "INSERT INTO mx_user (uid,uname,upwd) VALUES (null,?,?)";
			pool.query(sql1, [uname, upwd], (err, result) => {
				if (err) {
					console.log(err);
				}
				res.send({ code: 200, msg: "注册成功" });
			})
		}
	})
})
//用户登录
router.post("/login", (req, res) => {
	var { uname, upwd } = req.body;
	var sql = "select * from mx_user where uname=? and  upwd=?";
	pool.query(sql, [uname, upwd], (err, result) => {
		err && console.log(err);
		if (result.length > 0) {
			res.write(JSON.stringify({ ok: 1, uname: result[0].uname, uid: result[0].uid }));
		} else {
			res.write(JSON.stringify({ ok: 0, msg: "用户名或密码错误!" }));
		}
		res.end();
	})
})
module.exports = router;